/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.metadata.service.dev.core.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class GspMetadataThreadPoolFactory {
    // 私有构造函数，防止外部实例化
    private GspMetadataThreadPoolFactory() {}

    /**
     * Restore线程池单例类
     */
    private static class RestoreThreadPoolHolder {
        private static final ExecutorService executor = new ThreadPoolExecutor(2, 2,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(64),
                new ThreadFactory() {
                    private final AtomicInteger integer = new AtomicInteger(0);
                    @Override
                    public Thread newThread(Runnable r) {
                        Thread thread = new Thread(r);
                        thread.setName("lcm-restore-mdpkg-" + integer.incrementAndGet());
                        return thread;
                    }
                });

    }

    /**
     * InstallMdpkg线程池单例类
     */
    private static class InstallMdpkgThreadPoolHolder {
        private static final ExecutorService executor = new ThreadPoolExecutor(1, 1,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(64),
                new ThreadFactory() {
                    private final AtomicInteger integer = new AtomicInteger(0);
                    @Override
                    public Thread newThread(Runnable r) {
                        Thread thread = new Thread(r);
                        thread.setName("lcm-install-mdpkg-" + integer.incrementAndGet());
                        return thread;
                    }
                });

    }

    /**
     * Restart线程池单例类
     */
    private static class RestartThreadPoolHolder {
        private static final ExecutorService executor = new ThreadPoolExecutor(0, 64,
                60L, TimeUnit.SECONDS,
                new SynchronousQueue<>(),
                new ThreadFactory() {
                    private final AtomicInteger integer = new AtomicInteger(0);
                    @Override
                    public Thread newThread(Runnable r) {
                        Thread thread = new Thread(r);
                        thread.setName("lcm-deploy-and-restart-" + integer.incrementAndGet());
                        return thread;
                    }
                });

    }

    /**
     * 获取Restore操作单例线程池的方法
     * @return ExecutorService 实例
     */
    public static ExecutorService getRestoreExecutorService() {
        return RestoreThreadPoolHolder.executor;
    }
    /**
     * 获取InstallMdpkg操作单例线程池的方法
     * @return ExecutorService 实例
     */
    public static ExecutorService getInstallMdpkgExecutorService() {
        return InstallMdpkgThreadPoolHolder.executor;
    }
    /**
     * 获取Restart操作单例线程池的方法
     * @return ExecutorService 实例
     */
    public static ExecutorService getRestartExecutorService() {
        return RestartThreadPoolHolder.executor;
    }
}
